package com.cqjtu.eecs.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cqjtu.eecs.entity.Project;
import com.cqjtu.eecs.entity.params.project.ProjectUpdateParams;
import com.cqjtu.eecs.exception.ParamsErrorException;
import com.cqjtu.eecs.service.IProjectService;
import com.cqjtu.eecs.utils.Assert;
import com.cqjtu.eecs.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 培养方案 前端控制器
 * </p>
 *
 * @author
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/project")
@Api(tags = "年级培养方案")
public class ProjectController {
    @Autowired
    private IProjectService projectService;

    @ApiOperation("通过专业代码和年级获取培养方案")
    @GetMapping("/getProjectByCodeAndGrade/{majorCode}/{year}")
    public Project getProjectByIdAndYear(@PathVariable("majorCode")String majorCode, @PathVariable("year")String year)
    {
        return projectService.getOne(new QueryWrapper<Project>().eq("major_code",majorCode).eq("grade",year));
    }


    @ApiOperation("通过专业代码和年级删除培养方案")
    @DeleteMapping("/deleteById/{majorCode}/{year}")
    public Result<Object> deleteByIdAndYear(@PathVariable("majorCode")String majorCode, @PathVariable("year")String year){
        Assert.notBlank(majorCode, new ParamsErrorException("专业代码不能为空"));
        Assert.notBlank(year, new ParamsErrorException("年级不能为空"));
        if (projectService.remove(new QueryWrapper<Project>().eq("major_code",majorCode).eq("grade",year)))
            return Result.success();
        return Result.error(-1, "不存在");
    }

    @ApiOperation("修改培养方案")
    @PutMapping("/update")
    public Result<Object> update(@Valid @RequestBody ProjectUpdateParams projectUpdateParams){
        Project project1=new Project();
        BeanUtils.copyProperties(projectUpdateParams, project1);
        UpdateWrapper<Project> wrapper = new UpdateWrapper<>();
        if (projectService.update(new LambdaUpdateWrapper<Project>()
                .eq(Project::getMajorCode, project1.getMajorCode())
                .eq(Project::getGrade, project1.getGrade())
                .set(project1.getVersionCode() != null, Project::getVersionCode, project1.getVersionCode())
                .set(project1.getVersionName() != null, Project::getVersionName, project1.getVersionName())
                .set(project1.getGraduateCredit() != null, Project::getGraduateCredit, project1.getGraduateCredit())
                .set(project1.getTrainingGoalBrief() != null, Project::getTrainingGoalBrief, project1.getTrainingGoalBrief())
                .set(project1.getBasicYear()!=null,Project::getBasicYear,project1.getBasicYear())
        ))
            return Result.success();
        return Result.error();
    }


    @ApiOperation("修改多个培养方案")
    @PutMapping("/updateList")
    public  Result<Object> updateList(@Valid @RequestBody List<ProjectUpdateParams> projectUpdateParamsList)
    {
        for (ProjectUpdateParams i:projectUpdateParamsList
             ) {
            Project project1=new Project();
            BeanUtils.copyProperties(i, project1);
            UpdateWrapper<Project> wrapper = new UpdateWrapper<>();
            if (projectService.update(new LambdaUpdateWrapper<Project>()
                    .eq(Project::getGrade, project1.getGrade())
                    .eq(Project::getMajorCode, project1.getMajorCode())
                    .set(project1.getVersionCode() != null, Project::getVersionCode, project1.getVersionCode())
                    .set(project1.getVersionName() != null, Project::getVersionName, project1.getVersionName())
                    .set(project1.getGraduateCredit() != null, Project::getGraduateCredit, project1.getGraduateCredit())
                    .set(project1.getTrainingGoalBrief() != null, Project::getTrainingGoalBrief, project1.getTrainingGoalBrief())
                    .set(project1.getBasicYear()!=null,Project::getBasicYear,project1.getBasicYear())
            ))
                continue;
            else
                return Result.error();
        }
        return Result.success();
    }
    @ApiOperation("通过专业代码和年级复制培养方案")
    @GetMapping("/copy/{majorCode}/{year}")
    public Result copyProject(@PathVariable("majorCode")String majorCode, @PathVariable("year")Integer year){
        Assert.notBlank(majorCode, new ParamsErrorException("The majorCode can not be null or blank"));
        Assert.notNull(year, new ParamsErrorException("The year can not be null"));
        projectService.copyProject(majorCode, year);
        return Result.success();
    }

//    @PreAuthorize("hasRole('major_manger')")
//    @ApiOperation("系主任-获取当前系主任的培养方案")
//    @GetMapping("/getProjectByLoginUser")
//    public Project getProjectByLoginUser(Principal principal){
//        String nickname = principal.getNickname();
//        Teacher teacher = teacherService.getOne(new LambdaQueryWrapper<Teacher>().eq(Teacher::getTeacherCode, nickname));
//        Assert.notNull(teacher, new NotFoundException("Not find the user"));
//        List<Major> majorList = majorService.list(new LambdaQueryWrapper<Major>().eq(Major::getTid, teacher.getTid()));
//        if (majorList.isEmpty())
//            return new Project();
//
//    }


}
